Imports System
Imports System.Data
Imports System.Data.OleDb

Public Class frmMain
  Inherits System.Windows.Forms.Form

  Private Const MAX_RECORDS As Integer = 10000

  Private RecordsRead As Long

  Private strConnect As String
  Private MyTable As String
  Private MyDataSet As New DataSet()
  Private Function GetFileName() As String
    ' Przeznaczenie: Ta funkcja prbuje otworzy baz danych Access 
    '
    ' Lista argumentw:
    '  brak
    '
    ' Zwracana warto
    '  string   nazwa wskazanego pliku albo pusty acuch znakw 
    '        w przypadku anulowania.
    '
    Dim NewFileName As String
    Dim NewFile As OpenFileDialog = New OpenFileDialog()
    Dim MyChoice As DialogResult

    With NewFile
      .Filter = "Access Files(*.mdb)|*.mdb" & _
                "|All Files (*.*)|*.*"
      .FilterIndex = 1        ' Przyjmujemy domylnie pliki bazy Access
      .DefaultExt = "mdb"     ' Jak wyej
      .InitialDirectory = "D:\VSNET\Crystal Reports\Samples\Database\"
      .CheckFileExists = False
      .ReadOnlyChecked = True
      .Title = "Open Access Data File"
    End With

    MyChoice = NewFile.ShowDialog      ' Co wybra uytkownik?

    If MyChoice = DialogResult.Cancel Then ' Zrezygnowa?
      NewFileName = ""
    Else
      NewFileName = NewFile.FileName
    End If

    If NewFileName.Length > 0 Then
      Return NewFileName
    End If
  End Function

  Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles MyBase.Load
    ' Przeznaczenie: Otwarcie bazy danych i przeczytanie z niej nazw 
    ' tabel.
    Dim FileName, SQL As String

    FileName = GetFileName()      ' Pobierz nazw pliku bazy danych
    If FileName.Length = 0 Then
      Exit Sub
    End If
    Me.Text = "SQL Tester: " & FileName ' Poka nazw pliku

    ' Tu budujemy acuch poczenia. Bdzie on uywany rwnie pniej.
    strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User" & _
             "ID=Admin;Data Source="
    strConnect &= FileName
    MyTable = "Customer"
    Dim Connect As New OleDbConnection(strConnect)' Uywamy ponownie   
                                                    ' acucha poczenia...
    Connect.Open()              ' ...i otwieramy je jeszcze raz
    SQL = "SELECT * FROM Customer"
    Dim MyAdapter As New OleDbDataAdapter(SQL, Connect)

    Try
      ' Ile rekordw zostao zwrconych?
      RecordsRead = MyAdapter.Fill(MyDataSet, 0, MAX_RECORDS, MyTable)
      Connect.Close()
      SetControlBindings()
      MyAdapter = Nothing      ' Zwalniamy zasoby
      Connect = Nothing
    Catch
      MessageBox.Show("Something wrong during connecting.")
                 ' Poczenie nie powiodo si.
    End Try
    WhichRecord()

  End Sub

  Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles btnExit.Click
    Me.Dispose()
  End Sub

  Private Sub SetControlBindings()
    txtCompany.DataBindings.Add("Text", MyDataSet, "Customer.Customer _
            Name")
    txtFirstName.DataBindings.Add("Text", MyDataSet, _
            "Customer.Contact First Name")
    txtLastName.DataBindings.Add("Text", MyDataSet, _
            "Customer.Contact Last Name")
    txtAddress.DataBindings.Add("Text", MyDataSet, "Customer.Address1")
    txtCity.DataBindings.Add("Text", MyDataSet, "Customer.City")
    txtState.DataBindings.Add("Text", MyDataSet, "Customer.Region")
    txtZip.DataBindings.Add("Text", MyDataSet, "Customer.Postal Code")
  End Sub

  Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As _
                  System.EventArgs) Handles btnFirst.Click
    Me.BindingContext(MyDataSet, MyTable).Position = 0
    WhichRecord()
  End Sub

  Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles btnNext.Click
    Me.BindingContext(MyDataSet, MyTable).Position += 1
    WhichRecord()
  End Sub

  Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As _
                   System.EventArgs) Handles btnPrevious.Click
    Me.BindingContext(MyDataSet, MyTable).Position -= 1
    WhichRecord()
  End Sub

  Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles btnLast.Click
    Me.BindingContext(MyDataSet, MyTable).Position = RecordsRead - 1
    WhichRecord()
  End Sub

  Private Sub WhichRecord()
    ' Przeznaczenie: Podprogram ten ustala, ktrego rekordu szukamy 
    ' i wpisuje numer rekordu do waciwoci text pola grupy.
    Dim where As Long

    where = Me.BindingContext(MyDataSet, MyTable).Position + 1
    gbxDBNavigation.Text = "Navigate database: Record #" & CStr(where)

  End Sub

End Class
